/*
    GameButtons extends JPanel
        :Side panel of buttons that changes depending on the game state.
        :It is part of the GameView.
        Attributes
        Methods
*/

//Game Buttons is the collection of common game actions that should be readily available to the user.

//import only the things we need
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.Image;
//import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.File;
import javax.imageio.ImageIO;
import java.util.ArrayList;

public class GameButtons extends JPanel implements ActionListener {
	
	//declare some objects we will need around after the constructor finishes
	public JButton showMoves;
	//public JButton confirmMove;
	public JButton showReplay;
	public JButton forfeitGame;
	public JButton callDraw;
	public JButton placeChecker;
	public JButton placeKing;
	public JButton placeMine;
	public JButton placeBarrier;
	public JButton placeSafeZone;
	public JPanel setup;
	public JPanel play;
	
	//constructor
	public GameButtons() {
		
		//call our ancestor
		super();
		
		//declare temporary variables
		Image img = null;
		int scalex = 25;
		int scaley = 25;
		
		//setup our buttons
		//setup the showMoves button
		showMoves = new JButton();
		showMoves.addActionListener(this);
		showMoves.setText("Show Move(s)");
		showMoves.setVerticalTextPosition(SwingConstants.BOTTOM);
		showMoves.setHorizontalTextPosition(SwingConstants.CENTER);
		try{
			img = ImageIO.read(new File("images\\ShowMovesIcon.png"))
					.getScaledInstance(scalex,scaley,Image.SCALE_SMOOTH);
		}catch (IOException EXC){
			//if icon cannot be setup, use default
			img = new UIController.NotFoundImage(25,25);
		}catch (IllegalArgumentException EXC){
			//something went wrong, use default
			img = new UIController.NotFoundImage(25,25);
		}
		showMoves.setIcon(new ImageIcon(img));
		/*
		//setup the confirmMove button
		confirmMove = new JButton();
		confirmMove.addActionListener(this);
		confirmMove.setText("Confirm Move");
		confirmMove.setVerticalTextPosition(SwingConstants.BOTTOM);
		confirmMove.setHorizontalTextPosition(SwingConstants.CENTER);
		try{
			img = ImageIO.read(new File("images\\ConfirmMoveIcon.png"))
					.getScaledInstance(scalex,scaley,Image.SCALE_SMOOTH);
		}catch (IOException EXC){
			//if icon cannot be setup, use default
			img = new UIController.NotFoundImage(25,25);
		}catch (IllegalArgumentException EXC){
			//something went wrong, use default
			img = new UIController.NotFoundImage(25,25);
		}
		confirmMove.setIcon(new ImageIcon(img));
		*/
		//setup the showReplay button
		showReplay = new JButton();
		showReplay.addActionListener(this);
		showReplay.setText("Show Replay");
		showReplay.setVerticalTextPosition(SwingConstants.BOTTOM);
		showReplay.setHorizontalTextPosition(SwingConstants.CENTER);
		//enabled in Build 2
		showReplay.setEnabled(true);
		try{
			img = ImageIO.read(new File("images\\ShowReplayIcon.png"))
					.getScaledInstance(scalex,scaley,Image.SCALE_SMOOTH);
		}catch (IOException EXC){
			//if icon cannot be setup, use default
			img = new UIController.NotFoundImage(25,25);
		}catch (IllegalArgumentException EXC){
			//something went wrong, use default
			img = new UIController.NotFoundImage(25,25);
		}
		showReplay.setIcon(new ImageIcon(img));
		//setup the forfeitGame button
		forfeitGame = new JButton();
		forfeitGame.addActionListener(this);
		forfeitGame.setText("Forfeit Game");
		forfeitGame.setVerticalTextPosition(SwingConstants.BOTTOM);
		forfeitGame.setHorizontalTextPosition(SwingConstants.CENTER);
		//enabled by Build 3
		forfeitGame.setEnabled(true);
		try{
			img = ImageIO.read(new File("images\\ForfeitGameIcon.png"))
					.getScaledInstance(scalex,scaley,Image.SCALE_SMOOTH);
		}catch (IOException EXC){
			//if icon cannot be setup, use default
			img = new UIController.NotFoundImage(25,25);
		}catch (IllegalArgumentException EXC){
			//something went wrong, use default
			img = new UIController.NotFoundImage(25,25);
		}
		forfeitGame.setIcon(new ImageIcon(img));
		//setup the callDraw button
		callDraw = new JButton();
		callDraw.addActionListener(this);
		callDraw.setText("Call Draw");
		callDraw.setVerticalTextPosition(SwingConstants.BOTTOM);
		callDraw.setHorizontalTextPosition(SwingConstants.CENTER);
		//enabled by Build 3
		callDraw.setEnabled(true);
		try{
			img = ImageIO.read(new File("images\\CallDrawIcon.png"))
					.getScaledInstance(scalex,scaley,Image.SCALE_SMOOTH);
		}catch (IOException EXC){
			//if icon cannot be setup, use default
			img = new UIController.NotFoundImage(25,25);
		}catch (IllegalArgumentException EXC){
			//something went wrong, use default
			img = new UIController.NotFoundImage(25,25);
		}
		callDraw.setIcon(new ImageIcon(img));
		//setup the placeChecker button
		placeChecker = new JButton();
		placeChecker.addActionListener(this);
		placeChecker.setText("Checker\n\t(0)");
		placeChecker.setVerticalTextPosition(SwingConstants.BOTTOM);
		placeChecker.setHorizontalTextPosition(SwingConstants.CENTER);
		try{
			img = ImageIO.read(new File("images\\PlaceCheckerIcon.png"))
					.getScaledInstance(scalex,scaley,Image.SCALE_SMOOTH);
		}catch (IOException EXC){
			//if icon cannot be setup, use default
			img = new UIController.NotFoundImage(25,25);
		}catch (IllegalArgumentException EXC){
			//something went wrong, use default
			img = new UIController.NotFoundImage(25,25);
		}
		placeChecker.setIcon(new ImageIcon(img));
		//setup the placeKing button
		placeKing = new JButton();
		placeKing.addActionListener(this);
		placeKing.setText("King\n\t(0)");
		placeKing.setVerticalTextPosition(SwingConstants.BOTTOM);
		placeKing.setHorizontalTextPosition(SwingConstants.CENTER);
		try{
			img = ImageIO.read(new File("images\\PlaceKingIcon.png"))
					.getScaledInstance(scalex,scaley,Image.SCALE_SMOOTH);
		}catch (IOException EXC){
			//if icon cannot be setup, use default
			img = new UIController.NotFoundImage(25,25);
		}catch (IllegalArgumentException EXC){
			//something went wrong, use default
			img = new UIController.NotFoundImage(25,25);
		}
		placeKing.setIcon(new ImageIcon(img));
		//setup the placeMine button
		placeMine = new JButton();
		placeMine.addActionListener(this);
		placeMine.setText("Mine\n\t(0)");
		placeMine.setVerticalTextPosition(SwingConstants.BOTTOM);
		placeMine.setHorizontalTextPosition(SwingConstants.CENTER);
		//enabled by Build 3
		placeMine.setEnabled(true);
		try{
			img = ImageIO.read(new File("images\\PlaceMineIcon.png"))
					.getScaledInstance(scalex,scaley,Image.SCALE_SMOOTH);
		}catch (IOException EXC){
			//if icon cannot be setup, use default
			img = new UIController.NotFoundImage(25,25);
		}catch (IllegalArgumentException EXC){
			//something went wrong, use default
			img = new UIController.NotFoundImage(25,25);
		}
		placeMine.setIcon(new ImageIcon(img));
		//setup the placeBarrier button
		placeBarrier = new JButton();
		placeBarrier.addActionListener(this);
		placeBarrier.setText("BlockedZone\n\t(0)");
		placeBarrier.setVerticalTextPosition(SwingConstants.BOTTOM);
		placeBarrier.setHorizontalTextPosition(SwingConstants.CENTER);
		//enabled by Build 3
		placeBarrier.setEnabled(true);
		try{
			img = ImageIO.read(new File("images\\PlaceBarrierIcon.png"))
					.getScaledInstance(scalex,scaley,Image.SCALE_SMOOTH);
		}catch (IOException EXC){
			//if icon cannot be setup, use default
			img = new UIController.NotFoundImage(25,25);
		}catch (IllegalArgumentException EXC){
			//something went wrong, use default
			img = new UIController.NotFoundImage(25,25);
		}
		placeBarrier.setIcon(new ImageIcon(img));
		//setup the placeSafeZone button
		placeSafeZone = new JButton();
		placeSafeZone.addActionListener(this);
		placeSafeZone.setText("SafeZone\n\t(0)");
		placeSafeZone.setVerticalTextPosition(SwingConstants.BOTTOM);
		placeSafeZone.setHorizontalTextPosition(SwingConstants.CENTER);
		//enabled by Build 3
		placeSafeZone.setEnabled(true);
		try{
			img = ImageIO.read(new File("images\\PlaceSafeZoneIcon.png"))
					.getScaledInstance(scalex,scaley,Image.SCALE_SMOOTH);
		}catch (IOException EXC){
			//if icon cannot be setup, use default
			img = new UIController.NotFoundImage(25,25);
		}catch (IllegalArgumentException EXC){
			//something went wrong, use default
			img = new UIController.NotFoundImage(25,25);
		}
		placeSafeZone.setIcon(new ImageIcon(img));
		
		//setup our button setup panel
		//use a grid layout with 6 rows and 1 column
		//for design, we want to add 5x3 gap around each grid cell
		setup = new JPanel();
		setup.setLayout(new GridLayout(6,1,5,3));
		//add a simple label to the top indicate the mode
		JPanel setupP = new JPanel();
		setupP.setBackground(new Color(0, 169, 254));
		JLabel setupdescription = new JLabel();
		setupdescription.setText("<html><b><font size=\"4\">Game<br>Setup</font></b></html>");
		setupdescription.setHorizontalAlignment(SwingConstants.CENTER);
		setupP.add(setupdescription);
		setup.add(setupP);
		//add the setup buttons
		setup.add(placeChecker);
		setup.add(placeKing);
		setup.add(placeMine);
		setup.add(placeBarrier);
		setup.add(placeSafeZone);
		setup.setVisible(true);
		
		//setup our button play panel
		//use a grid layout with 5 rows and 1 column
		//for design, we want to add 5x3 gap around each grid cell
		play = new JPanel();
		play.setLayout(new GridLayout(5,1,5,3));
		//add a simple label to the top indicate the mode
		JPanel playP = new JPanel();
		playP.setBackground(new Color(25, 122, 48));
		JLabel playdescription = new JLabel();
		playdescription.setText("<html><b><font size=\"4\">Game<br>Play</font></b></html>");
		playdescription.setHorizontalAlignment(SwingConstants.CENTER);
		playP.add(playdescription);
		play.add(playP);
		//add the setup buttons
		play.add(showMoves);
		//play.add(confirmMove);
		play.add(showReplay);
		play.add(forfeitGame);
		play.add(callDraw);
		//set visible
		play.setVisible(true);
		
		//display setup by default
		this.displayGameSetup();
		
		//set a reasonable size to match the ratio of our splash image
		this.setPreferredSize(new Dimension(100, 350));
		
		//setup visibilities
		this.setVisible(true);
		this.setOpaque(true);
		
	}
	
	//method to signal that the Game Setup buttons should be displayed
	public void displayGameSetup() {
		
		this.removeAll();
		this.add(setup);
		this.revalidate();
		this.repaint();
		
	}
	
	//method to signal that the Game Play buttons should be displayed
	public void displayGamePlay() {
		
		this.removeAll();
		this.add(play);
		this.revalidate();
		this.repaint();
		
	}
	
	//method to update the text of the show moves button
	// to indicate if the option is turned on or off
	public void updateShowMoves(boolean on) {
		
		if (on){
			showMoves.setText("Show Move(s) On");
		}else{
			showMoves.setText("Show Move(s) Off");
		}
		
	}
	
	//method to update the text of the show replay button
	// to indicate if the option is turned on or off
	public void updateShowReplay(boolean on) {
		
		if (on){
			showReplay.setText("Show Replay On");
		}else{
			showReplay.setText("Show Replay Off");
		}
		
	}
	
	//method to update the game setup buttons' text to reflect the number
	//  of peices that can still be placed
	//this is designed to promote maintainability, so the text set in the constructor is preserved
	//  it assumes there is some text followed by "(", then the piece count, and then ")"
	public void updatePieceCounts(int checkers, int kings, int mines, int barriers, int safezones) {
		
		String[] tempsplits = null;
		String temptext = null;
		//update checker
		tempsplits = placeChecker.getText().split("\\(");
		temptext = (tempsplits[0] + "(" + checkers + ")");
		placeChecker.setText(temptext);
		//update king
		tempsplits = placeKing.getText().split("\\(");
		temptext = (tempsplits[0] + "(" + kings + ")");
		placeKing.setText(temptext);
		//update mine
		tempsplits = placeMine.getText().split("\\(");
		temptext = (tempsplits[0] + "(" + mines + ")");
		placeMine.setText(temptext);
		//update barrier
		tempsplits = placeBarrier.getText().split("\\(");
		temptext = (tempsplits[0] + "(" + barriers + ")");
		placeBarrier.setText(temptext);
		//update safezone
		tempsplits = placeSafeZone.getText().split("\\(");
		temptext = (tempsplits[0] + "(" + safezones + ")");
		placeSafeZone.setText(temptext);
		
	}
	//retrieve the piece counts for the current player from UI controller
	// then update our piece counts
	// return true if the current player's piece counts are not all 0
	public boolean getPieceCounts() {
		
		boolean notzero = true;
		ArrayList<Integer> counts = UIController.getPieceCounts();
		//check if all 0
		for (int i=0; i<counts.size(); ++i){
			notzero = notzero && (counts.get(i).intValue() == 0);
		}
		notzero = !notzero;
		//update our buttons
		this.updatePieceCounts(counts.get(0), counts.get(1), 
			counts.get(2), counts.get(3), counts.get(4));
			
		return notzero;
		
	}
	
	//overridden action listener method
	public void actionPerformed(ActionEvent EVE) {
		
		//find out who was activated
		Object tmpobj = EVE.getSource();
		//compare with known objects to find the culprit
		if (tmpobj.equals(placeChecker)){
			//GameView.gameboard.placeChecker();
			((GameView)UIController.grabState("GameView")).gameboard.placeChecker();
		}else if (tmpobj.equals(placeKing)){
			//GameView.gameboard.placeKing();
			((GameView)UIController.grabState("GameView")).gameboard.placeKing();
		}else if (tmpobj.equals(placeMine)){
			//GameView.gameboard.placeMine();
			((GameView)UIController.grabState("GameView")).gameboard.placeMine();
		}else if (tmpobj.equals(placeBarrier)){
			//GameView.gameboard.placeBlockedZone();
			((GameView)UIController.grabState("GameView")).gameboard.placeBlockedZone();
		}else if (tmpobj.equals(placeSafeZone)){
			//GameView.gameboard.placeSafeZone();
			((GameView)UIController.grabState("GameView")).gameboard.placeSafeZone();
		}else if (tmpobj.equals(showMoves)){
			//GameView.gameboard.showMoves();
			((GameView)UIController.grabState("GameView")).gameboard.showMoves();
		}else if (tmpobj.equals(showReplay)){
			//GameView.gameboard.showReplay();
			((GameView)UIController.grabState("GameView")).gameboard.showReplay();
		}else if (tmpobj.equals(forfeitGame)){
			//GameView.gameboard.forfeitGame();
			((GameView)UIController.grabState("GameView")).gameboard.forfeitGame();
		}else if (tmpobj.equals(callDraw)){
			//GameView.gameboard.callDraw();
			((GameView)UIController.grabState("GameView")).gameboard.callDraw();
		}//else if (tmpobj.equals(confirmMove)){
			//GameView.gameboard.confirmMove();
		
	}
	
}